package com.yuexunit.operationman.nbsy.module.OperationInventory.adapter;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.TextView;

import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.module.OperationInventory.bean.GoodsBean;
import com.yuexunit.operationman.nbsy.widget.NoCountDialog;

import org.kymjs.kjframe.widget.AdapterHolder;
import org.kymjs.kjframe.widget.KJAdapter;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Created by work-jx on 2017/9/5.
 */
public class TempletGoodsAdapter extends KJAdapter<GoodsBean.ObjectEntity> {
    public ArrayList<GoodsBean.ObjectEntity> lists = new ArrayList<GoodsBean.ObjectEntity>();

    public ArrayList<GoodsBean.ObjectEntity> getGoodsValue() {
        return lists;
    }

    public TempletGoodsAdapter(AbsListView view, Collection<GoodsBean.ObjectEntity> mDatas, int itemLayoutId) {
        super(view, mDatas, itemLayoutId);
        lists = (ArrayList<GoodsBean.ObjectEntity>) mDatas;
    }

    @Override
    public void convert(AdapterHolder helper, final GoodsBean.ObjectEntity item, boolean isScrolling, final int position) {
        helper.setText(R.id.name,item.getItemName());
        final TextView subtract, add;
        subtract = helper.getView(R.id.subtract);
        add = helper.getView(R.id.add);
        final TextView num_01,num_03;
        num_01 = helper.getView(R.id.num_01);
        num_03 = helper.getView(R.id.num_03);

        num_01.setText(item.getCount() + "");
        num_03.setText(item.getCount() + "");

        subtract.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(item.getCount() > 0){
                    num_01.setText((item.getCount() - 1) + "");
                    num_03.setText((item.getCount() - 1) + "");
                    lists.get(position).setCount(item.getCount() - 1);
                }
            }
        });

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int count=Integer.valueOf(num_01.getText().toString());
                count++;
                num_01.setText(count + "");
                num_03.setText(count + "");
                lists.get(position).setCount(count);
            }
        });

        num_01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final NoCountDialog dialog = new NoCountDialog(mCxt).builder();
                dialog.setNum(lists.get(position).getCount());
                dialog.setCallBack(new NoCountDialog.ICallBack() {
                    @Override
                    public void callback(Bundle bundle) {
                        if(bundle != null){
                            num_01.setText(bundle.getString("newNum"));
                            num_03.setText(bundle.getString("newNum"));
                            lists.get(position).setCount(Integer.valueOf(bundle.getString("newNum")));
                        }
                        dialog.dismiss();
                    }
                });
                dialog.show();
            }
        });
    }
}
